<?php
namespace Goxob;

use Mail, View;

class Email {

    public static function send($template, $data, $recipient, $subject, $fromAddress = '')
    {
        $data['siteUrl'] = url('');
        $data['siteName'] = 'Goxob Store';

        if(empty($fromAddress)){
            $fromAddress = 'doanvuthuan@gmail.com';
        }

        if(isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
            require_once 'google/appengine/api/mail/Message.php';

            $body = View::make($template, $data)->render();

            try
            {
                $message = new google\appengine\api\mail\Message();
                $message->setSender($fromAddress);
                $message->addTo($recipient);
                $message->setSubject($subject);
                $message->setHtmlBody($body);
                $message->send();
            } catch (InvalidArgumentException $e) {
                return Redirect::to('/')->withErrors(trans('Error send email contact!'))->withInput()->send();
            }
        }
        else{
            if(empty($fromName)){
                $fromName = 'Goxob';
            }
            Mail::queue($template, $data, function($message) use($recipient, $subject, $fromAddress, $fromName)
            {
                $message->from($fromAddress, $fromName);
                $message->to($recipient)->subject($subject);
            });
        }


    }
}